package com.my.fo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.fo.entity.MusicOrganization;
import com.my.fo.service.MusicOrganizationService;
import com.my.fo.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/music-organization")
@RequiredArgsConstructor
public class MusicOrganizationController {

    private final MusicOrganizationService musicOrganizationService;

    @PostMapping
    public R<?> save(@RequestBody MusicOrganization musicOrganization) {
        if (musicOrganizationService.save(musicOrganization)) return R.ok("保存成功!");
        return R.error("保存失败!");
    }

    @PutMapping
    public R<?> update(@RequestBody MusicOrganization musicOrganization) {
        if (musicOrganizationService.updateById(musicOrganization)) return R.ok("保存成功!");
        return R.error("保存失败!");
    }

    @GetMapping("latest")
    public R<?> getLatest() {
        MusicOrganization musicOrganization = musicOrganizationService.getOne(new QueryWrapper<MusicOrganization>().last("limit 1"));
        if (musicOrganization == null) musicOrganizationService.save((musicOrganization = new MusicOrganization()));
        return R.ok(musicOrganization);
    }

    @GetMapping("{id}")
    public R<?> getById(@PathVariable Integer id) {
        return R.ok(musicOrganizationService.getById(id));
    }

    @DeleteMapping
    public R<?> deleteById(@RequestBody List<Long> ids) {
        if (musicOrganizationService.removeByIds(ids)) return R.ok("删除成功!");
        return R.error("删除失败!");
    }

    @PostMapping("/submit")
    public R<?> submit(@RequestBody MusicOrganization musicOrganization) {
        if (musicOrganizationService.saveOrUpdate(musicOrganization)) {
            musicOrganizationService.organization(musicOrganization.getId());
            return R.ok("提交成功!", musicOrganization);
        }
        return R.error("提交失败!");
    }

}
